﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using HdipCricketClub.Models;
using HdipCricketClub.ViewModels;
using System.Web.Mvc;

namespace HdipCricketClub.ViewModels
{
    public class ScorecardAbandonedFixtureEnterScoresBatAndBowlViewModel
    {
        public Fixture ScorecardFixture { get; set; }

        public ScorecardHeader ScorecardHeaderDetail { get; set; }

        public List<ClubFixturePlayer> ClubBatsmen { get; set; }

        public List<ClubFixturePlayer> ClubBowlers { get; set; }

        public ClubOtherFixtureDetail ClubOtherDetail { get; set; }

        public List<OpponentFixturePlayer> OpponentBatsmen { get; set; }

        public List<OpponentFixturePlayer> OpponentBowlers { get; set; }

        public OpponentOtherFixtureDetail OpponentOtherDetail { get; set; }

        public SelectList ClubPlayerSelectList { get; set; }

        public SelectList ClubBowlerSelectList { get; set; }

        public SelectList OpponentPlayerSelectList { get; set; }

        public SelectList OpponentBowlerSelectList { get; set; }

        public SelectList DismissalTypeSelectList { get; set; }


        // Methods

        CricketClubEntities db = new CricketClubEntities();

        FixtureRepository fixtureRepository = new FixtureRepository();


        public ScorecardAbandonedFixtureEnterScoresBatAndBowlViewModel ParseFormCollectionBattedFirst(FormCollection formCollection)
        {
            ScorecardAbandonedFixtureEnterScoresBatAndBowlViewModel scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel = new ScorecardAbandonedFixtureEnterScoresBatAndBowlViewModel();

            // Form Collection

            string[] formCollectionFixtureId = formCollection.GetValues("ScorecardFixture.fixture_id");
            string[] formCollectionResultId = formCollection.GetValues("ScorecardHeaderDetail.result_id");
            string[] formCollectionScorecardEntered = formCollection.GetValues("ScorecardHeaderDetail.scorecard_entered");
            string[] formCollectionWonToss = formCollection.GetValues("ScorecardHeaderDetail.won_toss");
            string[] formCollectionBattedFirst = formCollection.GetValues("ScorecardHeaderDetail.batted_first");
            string[] formCollectionBatted = formCollection.GetValues("ScorecardHeaderDetail.batted");
            string[] formCollectionBowled = formCollection.GetValues("ScorecardHeaderDetail.bowled");

            // parse fixture

            int fixtureId = Int32.Parse(formCollectionFixtureId[0]);
            scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ScorecardFixture = fixtureRepository.GetFixture(fixtureId);

            // parse scorecard header

            scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ScorecardHeaderDetail = new ScorecardHeader()
            {
                fixture_id = fixtureId
            };

            if (formCollectionResultId[0] != "")
            {
                scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ScorecardHeaderDetail.result_id = Int32.Parse(formCollectionResultId[0]);
            }
            if (formCollectionScorecardEntered[0] != "")
            {
                scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ScorecardHeaderDetail.scorecard_entered = bool.Parse(formCollectionScorecardEntered[0]);
            }
            if (formCollectionWonToss[0] != "")
            {
                scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ScorecardHeaderDetail.won_toss = bool.Parse(formCollectionWonToss[0]);
            }
            if (formCollectionBattedFirst[0] != "")
            {
                scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ScorecardHeaderDetail.batted_first = bool.Parse(formCollectionBattedFirst[0]);
            }
            if (formCollectionBatted[0] != "")
            {
                scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ScorecardHeaderDetail.batted = bool.Parse(formCollectionBatted[0]);
            }
            if (formCollectionBowled[0] != "")
            {
                scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ScorecardHeaderDetail.bowled = bool.Parse(formCollectionBowled[0]);
            }

            // parse club players

            string[] playerFixtureId = formCollection.GetValues("item.fixture_id");
            string[] playerId = formCollection.GetValues("item.player_id");
            string[] batsmanNumber = formCollection.GetValues("item.batsman_number");
            string[] howOut = formCollection.GetValues("item.how_out");
            string[] fielderName = formCollection.GetValues("item.fielder_name");
            string[] bowlerName = formCollection.GetValues("item.bowler_name");
            string[] battingRuns = formCollection.GetValues("item.batting_runs");
            string[] bowlerNumber = formCollection.GetValues("item.bowler_number");
            string[] overs = formCollection.GetValues("item.overs");
            string[] maidens = formCollection.GetValues("item.maidens");
            string[] bowlingRuns = formCollection.GetValues("item.bowling_runs");
            string[] wickets = formCollection.GetValues("item.wickets");

            // club batsmen

            List<ClubFixturePlayer> scorecardClubPlayers = new List<ClubFixturePlayer>();

            for (int i = 0; i < scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ScorecardFixture.no_of_players; i++)
            {
                int currentFixture = Int32.Parse(playerFixtureId[i]);
                int currentPlayer = Int32.Parse(playerId[i]);

                ClubFixturePlayer clubBatsman = db.ClubFixturePlayers.Where(cfp => cfp.fixture_id == currentFixture && cfp.player_id == currentPlayer).Single();

                if (batsmanNumber[i] != "")
                {
                    clubBatsman.batsman_number = Int32.Parse(batsmanNumber[i]);
                }

                if (howOut[i] != "")
                {
                    clubBatsman.how_out = Int32.Parse(howOut[i]);
                }

                if (fielderName[i] != "")
                {
                    clubBatsman.fielder_name = fielderName[i];
                }

                if (bowlerName[i] != "")
                {
                    clubBatsman.bowler_name = bowlerName[i];
                }

                if (battingRuns[i] != "")
                {
                    clubBatsman.batting_runs = double.Parse(battingRuns[i]);
                }

                scorecardClubPlayers.Add(clubBatsman);
            }

            List<ClubFixturePlayer> sortedScorecardClubPlayers = scorecardClubPlayers.OrderBy(cfp => cfp.batsman_number).ToList();

            scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ClubBatsmen = sortedScorecardClubPlayers;

            // club bowlers

            List<ClubFixturePlayer> clubBowlers = new List<ClubFixturePlayer>();

            for (int j = scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ScorecardFixture.no_of_players; j < playerFixtureId.Length; j++)
            {
                int currentFixture = Int32.Parse(playerFixtureId[j]);
                int currentPlayer = Int32.Parse(playerId[j]);

                ClubFixturePlayer clubBowler = db.ClubFixturePlayers.Where(cfp => cfp.fixture_id == currentFixture && cfp.player_id == currentPlayer).Single();


                if (bowlerNumber[j - scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ScorecardFixture.no_of_players] != "")
                {
                    clubBowler.bowler_number = Int32.Parse(bowlerNumber[j - scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ScorecardFixture.no_of_players]);
                }

                if (overs[j - scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ScorecardFixture.no_of_players] != "")
                {
                    clubBowler.overs = double.Parse(overs[j - scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ScorecardFixture.no_of_players]);
                }

                if (maidens[j - scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ScorecardFixture.no_of_players] != "")
                {
                    clubBowler.maidens = double.Parse(maidens[j - scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ScorecardFixture.no_of_players]);
                }

                if (bowlingRuns[j - scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ScorecardFixture.no_of_players] != "")
                {
                    clubBowler.bowling_runs = double.Parse(bowlingRuns[j - scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ScorecardFixture.no_of_players]);
                }

                if (wickets[j - scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ScorecardFixture.no_of_players] != "")
                {
                    clubBowler.wickets = double.Parse(wickets[j - scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ScorecardFixture.no_of_players]);
                }

                clubBowlers.Add(clubBowler);
            }

            List<ClubFixturePlayer> sortedClubBowlers = clubBowlers.OrderBy(cfp => cfp.bowler_number).ToList();

            scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ClubBowlers = sortedClubBowlers;

            // club Other detail

            string[] formCollectionByes = formCollection.GetValues("ClubOtherDetail.byes");
            string[] formCollectionLegByes = formCollection.GetValues("ClubOtherDetail.leg_byes");
            string[] formCollectionWides = formCollection.GetValues("ClubOtherDetail.wides");
            string[] formCollectionNoBalls = formCollection.GetValues("ClubOtherDetail.no_balls");

            ClubOtherFixtureDetail clubOtherFixtureDetail = new ClubOtherFixtureDetail()
            {
                fixture_id = scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ScorecardFixture.fixture_id
            };

            if (formCollectionByes[0] != "")
            {
                clubOtherFixtureDetail.byes = double.Parse(formCollectionByes[0]);
            }

            if (formCollectionByes[0] == "")
            {
                clubOtherFixtureDetail.byes = 0;
            }

            if (formCollectionLegByes[0] != "")
            {
                clubOtherFixtureDetail.leg_byes = double.Parse(formCollectionLegByes[0]);
            }

            if (formCollectionLegByes[0] == "")
            {
                clubOtherFixtureDetail.leg_byes = 0;
            }

            if (formCollectionWides[0] != "")
            {
                clubOtherFixtureDetail.wides = double.Parse(formCollectionWides[0]);
            }

            if (formCollectionWides[0] == "")
            {
                clubOtherFixtureDetail.wides = 0;
            }

            if (formCollectionNoBalls[0] != "")
            {
                clubOtherFixtureDetail.no_balls = double.Parse(formCollectionNoBalls[0]);
            }

            if (formCollectionNoBalls[0] == "")
            {
                clubOtherFixtureDetail.no_balls = 0;
            }

            scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ClubOtherDetail = clubOtherFixtureDetail;

            // opponent players

            string[] opFixtureId = formCollection.GetValues("item.op_fixture_id");
            string[] opBatsmanNumber = formCollection.GetValues("item.op_batsman_number");
            string[] opBowlerNumber = formCollection.GetValues("item.op_bowler_number");
            string[] opPlayerName = formCollection.GetValues("item.op_player_name");
            string[] opOvers = formCollection.GetValues("item.op_overs");
            string[] opMaidens = formCollection.GetValues("item.op_maidens");
            string[] opBowlingRuns = formCollection.GetValues("item.op_bowling_runs");
            string[] opWickets = formCollection.GetValues("item.op_wickets");
            string[] opHowOut = formCollection.GetValues("item.op_how_out");
            string[] opFielderName = formCollection.GetValues("item.op_fielder_name");
            string[] opBowlerName = formCollection.GetValues("item.op_bowler_name");
            string[] opBattingRuns = formCollection.GetValues("item.op_batting_runs");

            // opponent bowlers

            List<OpponentFixturePlayer> opponentBowlers = new List<OpponentFixturePlayer>();

            for (int i = 0; i < opFixtureId.Length - scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ScorecardFixture.no_of_players; i++)
            {
                int batsmanId = Int32.Parse(opBatsmanNumber[i]);

                OpponentFixturePlayer opponentBowler = new OpponentFixturePlayer()
                {
                    op_fixture_id = scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ScorecardFixture.fixture_id,
                    op_batsman_number = batsmanId
                };

                if (opBowlerNumber[i] != "")
                {
                    opponentBowler.op_bowler_number = Int32.Parse(opBowlerNumber[i]);
                }

                if (opPlayerName[i] != "")
                {
                    opponentBowler.op_player_name = opPlayerName[i];
                }

                if (opOvers[i] != "")
                {
                    opponentBowler.op_overs = Int32.Parse(opOvers[i]);
                }

                if (opMaidens[i] != "")
                {
                    opponentBowler.op_maidens = Int32.Parse(opMaidens[i]);
                }

                if (opBowlingRuns[i] != "")
                {
                    opponentBowler.op_bowling_runs = Int32.Parse(opBowlingRuns[i]);
                }

                if (opWickets[i] != "")
                {
                    opponentBowler.op_wickets = Int32.Parse(opWickets[i]);
                }

                opponentBowlers.Add(opponentBowler);

            }

            List<OpponentFixturePlayer> sortedOpponentBowlers = opponentBowlers.OrderBy(ofp => ofp.op_bowler_number).ToList();

            scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.OpponentBowlers = sortedOpponentBowlers;

            // opponent Batsman

            List<OpponentFixturePlayer> scorecardOpponentPlayers = new List<OpponentFixturePlayer>();

            for (int i = opBowlerNumber.Length; i < opBatsmanNumber.Length; i++)
            {
                int currentFixture = Int32.Parse(opFixtureId[i]);
                int currentPlayerBatsmanNumber = Int32.Parse(opBatsmanNumber[i]);

                OpponentFixturePlayer opponentBatsman = new OpponentFixturePlayer
                {
                    op_fixture_id = currentFixture,
                    op_batsman_number = currentPlayerBatsmanNumber
                };

                if (opPlayerName[i] != "")
                {
                    opponentBatsman.op_player_name = opPlayerName[i];
                }

                if (opHowOut[i - opBowlerNumber.Length] != "")
                {
                    opponentBatsman.op_how_out = Int32.Parse(opHowOut[i - opBowlerNumber.Length]);
                }

                if (opFielderName[i - opBowlerNumber.Length] != "")
                {
                    opponentBatsman.op_fielder_name = Int32.Parse(opFielderName[i - opBowlerNumber.Length]);
                }

                if (opBowlerName[i - opBowlerNumber.Length] != "")
                {
                    opponentBatsman.op_bowler_name = Int32.Parse(opBowlerName[i - opBowlerNumber.Length]);
                }

                if (opBattingRuns[i - opBowlerNumber.Length] != "")
                {
                    opponentBatsman.op_batting_runs = double.Parse(opBattingRuns[i - opBowlerNumber.Length]);
                }

                scorecardOpponentPlayers.Add(opponentBatsman);
            }

            List<OpponentFixturePlayer> sortedScorecardOpponentPlayers = scorecardOpponentPlayers.OrderBy(ofp => ofp.op_batsman_number).ToList();

            scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.OpponentBatsmen = sortedScorecardOpponentPlayers;

            // club Other detail

            string[] formCollectionOpByes = formCollection.GetValues("OpponentOtherDetail.byes");
            string[] formCollectionOpLegByes = formCollection.GetValues("OpponentOtherDetail.leg_byes");
            string[] formCollectionOpWides = formCollection.GetValues("OpponentOtherDetail.wides");
            string[] formCollectionOpNoBalls = formCollection.GetValues("OpponentOtherDetail.no_balls");

            OpponentOtherFixtureDetail opponentOtherFixtureDetail = new OpponentOtherFixtureDetail()
            {
                fixture_id = scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ScorecardFixture.fixture_id
            };

            if (formCollectionOpByes[0] != "")
            {
                opponentOtherFixtureDetail.byes = double.Parse(formCollectionOpByes[0]);
            }

            if (formCollectionOpByes[0] == "")
            {
                opponentOtherFixtureDetail.byes = 0;
            }

            if (formCollectionOpLegByes[0] != "")
            {
                opponentOtherFixtureDetail.leg_byes = double.Parse(formCollectionOpLegByes[0]);
            }

            if (formCollectionOpLegByes[0] == "")
            {
                opponentOtherFixtureDetail.leg_byes = 0;
            }

            if (formCollectionOpWides[0] != "")
            {
                opponentOtherFixtureDetail.wides = double.Parse(formCollectionOpWides[0]);
            }

            if (formCollectionOpWides[0] == "")
            {
                opponentOtherFixtureDetail.wides = 0;
            }

            if (formCollectionOpNoBalls[0] != "")
            {
                opponentOtherFixtureDetail.no_balls = double.Parse(formCollectionOpNoBalls[0]);
            }

            if (formCollectionOpNoBalls[0] == "")
            {
                opponentOtherFixtureDetail.no_balls = 0;
            }

            scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.OpponentOtherDetail = opponentOtherFixtureDetail;

            return scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel;
        }

        public List<string> ValidateClubScorecardTotals(ScorecardAbandonedFixtureEnterScoresBatAndBowlViewModel scorecardData)
        {
            string message;

            List<string> errorMessages = new List<string>();

            int noOfBatsmanBatted = 0;
            int noOfNotOutBatsmen = 0;
            double totalBattingRuns = 0;
            double totalBowlerWickets = 0;

            foreach (ClubFixturePlayer batsman in scorecardData.ClubBatsmen)
            {
                if (batsman.how_out != 8)
                {
                    noOfBatsmanBatted += 1;
                }
                if (batsman.how_out == 9)
                {
                    noOfNotOutBatsmen += 1;
                }

                if (batsman.batting_runs != null)
                {
                    totalBattingRuns += (double)batsman.batting_runs;
                }

                if ((batsman.how_out != 6) && (batsman.how_out != 8) && (batsman.how_out != 9))
                {
                    totalBowlerWickets += 1;
                }
            }

            // Validate must be at least two batsmen who batted

            if (noOfBatsmanBatted < 2)
            {
                message = "At least two batsman must have batted";
                errorMessages.Add(message);
            }

            // Validate cannot be more than two not out batsmen

            if (noOfNotOutBatsmen > 2)
            {
                message = "There cannot be more than two not out batsmen";
                errorMessages.Add(message);
            }

            double totalBowlingRuns = 0;
            double totalWickets = 0;

            foreach (OpponentFixturePlayer bowler in scorecardData.OpponentBowlers)
            {
                if (bowler.op_bowling_runs != null)
                {
                    totalBowlingRuns += (double)bowler.op_bowling_runs;
                }

                if (bowler.op_wickets != null)
                {
                    totalWickets += (double)bowler.op_wickets;
                }
            }

            // Validate total batting runs + wides + no balls = total bowling runs

            if ((totalBattingRuns + scorecardData.ClubOtherDetail.wides + scorecardData.ClubOtherDetail.no_balls) != totalBowlingRuns)
            {
                message = "The total runs for batsmen + wides + no balls should equal total bowlers runs, batting runs = " + (totalBattingRuns + scorecardData.ClubOtherDetail.wides + scorecardData.ClubOtherDetail.no_balls) + " bowling runs = " + totalBowlingRuns;
                errorMessages.Add(message);
            }

            // Validate total wickets in bowling figures is equal to batting wickets lost Less run out, not out and did not bat

            if (totalBowlerWickets != totalWickets)
            {
                message = "Total wickets in bowlers figures should equal batsmen out excluding those run out";
                errorMessages.Add(message);
            }

            return errorMessages;
        }

        public List<string> ValidateOpponentScorecardTotals(ScorecardAbandonedFixtureEnterScoresBatAndBowlViewModel scorecardData)
        {
            string message;

            List<string> errorMessages = new List<string>();

            int noOfBatsmanBatted = 0;
            int noOfNotOutBatsmen = 0;
            double totalBattingRuns = 0;
            double totalBowlerWickets = 0;

            foreach (OpponentFixturePlayer batsman in scorecardData.OpponentBatsmen)
            {
                if (batsman.op_how_out != 8)
                {
                    noOfBatsmanBatted += 1;
                }
                if (batsman.op_how_out == 9)
                {
                    noOfNotOutBatsmen += 1;
                }

                if (batsman.op_batting_runs != null)
                {
                    totalBattingRuns += (double)batsman.op_batting_runs;
                }

                if ((batsman.op_how_out != 6) && (batsman.op_how_out != 8) && (batsman.op_how_out != 9))
                {
                    totalBowlerWickets += 1;
                }
            }

            // Validate must be at least two batsmen who batted

            if (noOfBatsmanBatted < 2)
            {
                message = "At least two batsman must have batted";
                errorMessages.Add(message);
            }

            // Validate cannot be more than two not out batsmen

            if (noOfNotOutBatsmen > 2)
            {
                message = "There cannot be more than two not out batsmen";
                errorMessages.Add(message);
            }

            double totalBowlingRuns = 0;
            double totalWickets = 0;

            if (scorecardData.ClubBowlers.Count() > 0)
            {
                foreach (ClubFixturePlayer bowler in scorecardData.ClubBowlers)
                {
                    if (bowler.bowling_runs != null)
                    {
                        totalBowlingRuns += (double)bowler.bowling_runs;
                    }

                    if (bowler.wickets != null)
                    {
                        totalWickets += (double)bowler.wickets;
                    }
                }
            }

            // Validate total batting runs + wides + no balls = total bowling runs

            if ((totalBattingRuns + scorecardData.OpponentOtherDetail.wides + scorecardData.OpponentOtherDetail.no_balls) != totalBowlingRuns)
            {
                message = "The total runs for batsmen + wides + no balls should equal total bowlers runs, batting runs = " + (totalBattingRuns + scorecardData.OpponentOtherDetail.wides + scorecardData.OpponentOtherDetail.no_balls) + " bowling runs = " + totalBowlingRuns;
                errorMessages.Add(message);
            }

            // Validate total wickets in bowling figures is equal to batting wickets lost Less run out, not out and did not bat

            if (totalBowlerWickets != totalWickets)
            {
                message = "Total wickets in bowlers figures should equal batsmen out excluding those run out";
                errorMessages.Add(message);
            }

            return errorMessages;
        }

        public ScorecardAbandonedFixtureEnterScoresBatAndBowlViewModel ParseFormCollectionBattedSecond(FormCollection formCollection)
        {
            ScorecardAbandonedFixtureEnterScoresBatAndBowlViewModel scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel = new ScorecardAbandonedFixtureEnterScoresBatAndBowlViewModel();

            // Form Collection

            string[] formCollectionFixtureId = formCollection.GetValues("ScorecardFixture.fixture_id");
            string[] formCollectionResultId = formCollection.GetValues("ScorecardHeaderDetail.result_id");
            string[] formCollectionScorecardEntered = formCollection.GetValues("ScorecardHeaderDetail.scorecard_entered");
            string[] formCollectionWonToss = formCollection.GetValues("ScorecardHeaderDetail.won_toss");
            string[] formCollectionBattedFirst = formCollection.GetValues("ScorecardHeaderDetail.batted_first");
            string[] formCollectionBatted = formCollection.GetValues("ScorecardHeaderDetail.batted");
            string[] formCollectionBowled = formCollection.GetValues("ScorecardHeaderDetail.bowled");

            // parse fixture

            int fixtureId = Int32.Parse(formCollectionFixtureId[0]);
            scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ScorecardFixture = fixtureRepository.GetFixture(fixtureId);

            // parse scorecard header

            scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ScorecardHeaderDetail = new ScorecardHeader()
            {
                fixture_id = fixtureId
            };

            if (formCollectionResultId[0] != "")
            {
                scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ScorecardHeaderDetail.result_id = Int32.Parse(formCollectionResultId[0]);
            }
            if (formCollectionScorecardEntered[0] != "")
            {
                scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ScorecardHeaderDetail.scorecard_entered = bool.Parse(formCollectionScorecardEntered[0]);
            }
            if (formCollectionWonToss[0] != "")
            {
                scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ScorecardHeaderDetail.won_toss = bool.Parse(formCollectionWonToss[0]);
            }
            if (formCollectionBattedFirst[0] != "")
            {
                scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ScorecardHeaderDetail.batted_first = bool.Parse(formCollectionBattedFirst[0]);
            }
            if (formCollectionBatted[0] != "")
            {
                scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ScorecardHeaderDetail.batted = bool.Parse(formCollectionBatted[0]);
            }
            if (formCollectionBowled[0] != "")
            {
                scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ScorecardHeaderDetail.bowled = bool.Parse(formCollectionBowled[0]);
            }

            // parse club players

            string[] playerFixtureId = formCollection.GetValues("item.fixture_id");
            string[] playerId = formCollection.GetValues("item.player_id");
            string[] batsmanNumber = formCollection.GetValues("item.batsman_number");
            string[] howOut = formCollection.GetValues("item.how_out");
            string[] fielderName = formCollection.GetValues("item.fielder_name");
            string[] bowlerName = formCollection.GetValues("item.bowler_name");
            string[] battingRuns = formCollection.GetValues("item.batting_runs");
            string[] bowlerNumber = formCollection.GetValues("item.bowler_number");
            string[] overs = formCollection.GetValues("item.overs");
            string[] maidens = formCollection.GetValues("item.maidens");
            string[] bowlingRuns = formCollection.GetValues("item.bowling_runs");
            string[] wickets = formCollection.GetValues("item.wickets");

            // club batsmen

            List<ClubFixturePlayer> scorecardClubPlayers = new List<ClubFixturePlayer>();

            for (int i = bowlerNumber.Length; i < playerId.Length; i++)
            {
                int currentFixture = Int32.Parse(playerFixtureId[i]);
                int currentPlayer = Int32.Parse(playerId[i]);

                ClubFixturePlayer clubBatsman = db.ClubFixturePlayers.Where(cfp => cfp.fixture_id == currentFixture && cfp.player_id == currentPlayer).Single();

                if (batsmanNumber[i - bowlerNumber.Length] != "")
                {
                    clubBatsman.batsman_number = Int32.Parse(batsmanNumber[i - bowlerNumber.Length]);
                }

                if (howOut[i - bowlerNumber.Length] != "")
                {
                    clubBatsman.how_out = Int32.Parse(howOut[i - bowlerNumber.Length]);
                }

                if (fielderName[i - bowlerNumber.Length] != "")
                {
                    clubBatsman.fielder_name = fielderName[i - bowlerNumber.Length];
                }

                if (bowlerName[i - bowlerNumber.Length] != "")
                {
                    clubBatsman.bowler_name = bowlerName[i - bowlerNumber.Length];
                }

                if (battingRuns[i - bowlerNumber.Length] != "")
                {
                    clubBatsman.batting_runs = double.Parse(battingRuns[i - bowlerNumber.Length]);
                }

                scorecardClubPlayers.Add(clubBatsman);
            }

            List<ClubFixturePlayer> sortedScorecardClubPlayers = scorecardClubPlayers.OrderBy(cfp => cfp.batsman_number).ToList();

            scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ClubBatsmen = sortedScorecardClubPlayers;

            // club bowlers

            List<ClubFixturePlayer> clubBowlers = new List<ClubFixturePlayer>();

            for (int j = 0; j < bowlerNumber.Length; j++)
            {
                int currentFixture = Int32.Parse(playerFixtureId[j]);
                int currentPlayer = Int32.Parse(playerId[j]);

                ClubFixturePlayer clubBowler = db.ClubFixturePlayers.Where(cfp => cfp.fixture_id == currentFixture && cfp.player_id == currentPlayer).Single();

                if (bowlerNumber[j] != "")
                {
                    clubBowler.bowler_number = Int32.Parse(bowlerNumber[j]);
                }

                if (overs[j] != "")
                {
                    clubBowler.overs = double.Parse(overs[j]);
                }

                if (maidens[j] != "")
                {
                    clubBowler.maidens = double.Parse(maidens[j]);
                }

                if (bowlingRuns[j] != "")
                {
                    clubBowler.bowling_runs = double.Parse(bowlingRuns[j]);
                }

                if (wickets[j] != "")
                {
                    clubBowler.wickets = double.Parse(wickets[j]);
                }

                clubBowlers.Add(clubBowler);
            }

            List<ClubFixturePlayer> sortedClubBowlers = clubBowlers.OrderBy(cfp => cfp.bowler_number).ToList();

            scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ClubBowlers = sortedClubBowlers;

            // club Other detail

            string[] formCollectionByes = formCollection.GetValues("ClubOtherDetail.byes");
            string[] formCollectionLegByes = formCollection.GetValues("ClubOtherDetail.leg_byes");
            string[] formCollectionWides = formCollection.GetValues("ClubOtherDetail.wides");
            string[] formCollectionNoBalls = formCollection.GetValues("ClubOtherDetail.no_balls");

            ClubOtherFixtureDetail clubOtherFixtureDetail = new ClubOtherFixtureDetail()
            {
                fixture_id = scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ScorecardFixture.fixture_id
            };

            if (formCollectionByes[0] != "")
            {
                clubOtherFixtureDetail.byes = double.Parse(formCollectionByes[0]);
            }

            if (formCollectionByes[0] == "")
            {
                clubOtherFixtureDetail.byes = 0;
            }

            if (formCollectionLegByes[0] != "")
            {
                clubOtherFixtureDetail.leg_byes = double.Parse(formCollectionLegByes[0]);
            }

            if (formCollectionLegByes[0] == "")
            {
                clubOtherFixtureDetail.leg_byes = 0;
            }

            if (formCollectionWides[0] != "")
            {
                clubOtherFixtureDetail.wides = double.Parse(formCollectionWides[0]);
            }

            if (formCollectionWides[0] == "")
            {
                clubOtherFixtureDetail.wides = 0;
            }

            if (formCollectionNoBalls[0] != "")
            {
                clubOtherFixtureDetail.no_balls = double.Parse(formCollectionNoBalls[0]);
            }

            if (formCollectionNoBalls[0] == "")
            {
                clubOtherFixtureDetail.no_balls = 0;
            }

            scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ClubOtherDetail = clubOtherFixtureDetail;

            // opponent players


            string[] opFixtureId = formCollection.GetValues("item.op_fixture_id");
            string[] opBatsmanNumber = formCollection.GetValues("item.op_batsman_number");
            string[] opBowlerNumber = formCollection.GetValues("item.op_bowler_number");
            string[] opPlayerName = formCollection.GetValues("item.op_player_name");
            string[] opOvers = formCollection.GetValues("item.op_overs");
            string[] opMaidens = formCollection.GetValues("item.op_maidens");
            string[] opBowlingRuns = formCollection.GetValues("item.op_bowling_runs");
            string[] opWickets = formCollection.GetValues("item.op_wickets");
            string[] opHowOut = formCollection.GetValues("item.op_how_out");
            string[] opFielderName = formCollection.GetValues("item.op_fielder_name");
            string[] opBowlerName = formCollection.GetValues("item.op_bowler_name");
            string[] opBattingRuns = formCollection.GetValues("item.op_batting_runs");

            // opponent bowlers

            List<OpponentFixturePlayer> opponentBowlers = new List<OpponentFixturePlayer>();

            for (int i = scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ScorecardFixture.no_of_players; i < opBatsmanNumber.Length; i++)
            {
                int batsmanId = Int32.Parse(opBatsmanNumber[i]);

                OpponentFixturePlayer opponentBowler = new OpponentFixturePlayer()
                {
                    op_fixture_id = scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ScorecardFixture.fixture_id,
                    op_batsman_number = batsmanId
                };

                if (opBowlerNumber[i - scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ScorecardFixture.no_of_players] != "")
                {
                    opponentBowler.op_bowler_number = Int32.Parse(opBowlerNumber[i - scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ScorecardFixture.no_of_players]);
                }

                if (opPlayerName[i - scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ScorecardFixture.no_of_players] != "")
                {
                    opponentBowler.op_player_name = opPlayerName[i - scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ScorecardFixture.no_of_players];
                }

                if (opOvers[i - scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ScorecardFixture.no_of_players] != "")
                {
                    opponentBowler.op_overs = Int32.Parse(opOvers[i - scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ScorecardFixture.no_of_players]);
                }

                if (opMaidens[i - scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ScorecardFixture.no_of_players] != "")
                {
                    opponentBowler.op_maidens = Int32.Parse(opMaidens[i - scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ScorecardFixture.no_of_players]);
                }

                if (opBowlingRuns[i - scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ScorecardFixture.no_of_players] != "")
                {
                    opponentBowler.op_bowling_runs = Int32.Parse(opBowlingRuns[i - scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ScorecardFixture.no_of_players]);
                }

                if (opWickets[i - scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ScorecardFixture.no_of_players] != "")
                {
                    opponentBowler.op_wickets = Int32.Parse(opWickets[i - scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ScorecardFixture.no_of_players]);
                }

                opponentBowlers.Add(opponentBowler);

            }

            List<OpponentFixturePlayer> sortedOpponentBowlers = opponentBowlers.OrderBy(ofp => ofp.op_bowler_number).ToList();

            scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.OpponentBowlers = sortedOpponentBowlers;

            // opponent Batsman

            List<OpponentFixturePlayer> scorecardOpponentPlayers = new List<OpponentFixturePlayer>();

            for (int i = 0; i < scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ScorecardFixture.no_of_players; i++)
            {
                int currentFixture = Int32.Parse(opFixtureId[i]);
                int currentPlayerBatsmanNumber = Int32.Parse(opBatsmanNumber[i]);

                OpponentFixturePlayer opponentBatsman = new OpponentFixturePlayer
                {
                    op_fixture_id = currentFixture,
                    op_batsman_number = currentPlayerBatsmanNumber
                };

                if (opPlayerName[i] != "")
                {
                    opponentBatsman.op_player_name = opPlayerName[i];
                }

                if (opHowOut[i] != "")
                {
                    opponentBatsman.op_how_out = Int32.Parse(opHowOut[i]);
                }

                if (opFielderName[i] != "")
                {
                    opponentBatsman.op_fielder_name = Int32.Parse(opFielderName[i]);
                }

                if (opBowlerName[i] != "")
                {
                    opponentBatsman.op_bowler_name = Int32.Parse(opBowlerName[i]);
                }

                if (opBattingRuns[i] != "")
                {
                    opponentBatsman.op_batting_runs = double.Parse(opBattingRuns[i]);
                }

                scorecardOpponentPlayers.Add(opponentBatsman);
            }

            List<OpponentFixturePlayer> sortedScorecardOpponentPlayers = scorecardOpponentPlayers.OrderBy(ofp => ofp.op_batsman_number).ToList();

            scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.OpponentBatsmen = sortedScorecardOpponentPlayers;

            // club Other detail

            string[] formCollectionOpByes = formCollection.GetValues("OpponentOtherDetail.byes");
            string[] formCollectionOpLegByes = formCollection.GetValues("OpponentOtherDetail.leg_byes");
            string[] formCollectionOpWides = formCollection.GetValues("OpponentOtherDetail.wides");
            string[] formCollectionOpNoBalls = formCollection.GetValues("OpponentOtherDetail.no_balls");

            OpponentOtherFixtureDetail opponentOtherFixtureDetail = new OpponentOtherFixtureDetail()
            {
                fixture_id = scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ScorecardFixture.fixture_id
            };

            if (formCollectionOpByes[0] != "")
            {
                opponentOtherFixtureDetail.byes = double.Parse(formCollectionOpByes[0]);
            }

            if (formCollectionOpByes[0] == "")
            {
                opponentOtherFixtureDetail.byes = 0;
            }

            if (formCollectionOpLegByes[0] != "")
            {
                opponentOtherFixtureDetail.leg_byes = double.Parse(formCollectionOpLegByes[0]);
            }

            if (formCollectionOpLegByes[0] == "")
            {
                opponentOtherFixtureDetail.leg_byes = 0;
            }

            if (formCollectionOpWides[0] != "")
            {
                opponentOtherFixtureDetail.wides = double.Parse(formCollectionOpWides[0]);
            }

            if (formCollectionOpWides[0] == "")
            {
                opponentOtherFixtureDetail.wides = 0;
            }

            if (formCollectionOpNoBalls[0] != "")
            {
                opponentOtherFixtureDetail.no_balls = double.Parse(formCollectionOpNoBalls[0]);
            }

            if (formCollectionOpNoBalls[0] == "")
            {
                opponentOtherFixtureDetail.no_balls = 0;
            }

            scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.OpponentOtherDetail = opponentOtherFixtureDetail;

            return scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel;
        }

        public ScorecardAbandonedFixtureEnterScoresBatAndBowlViewModel ParseFormCollectionBattedOnly(FormCollection formCollection)
        {
            ScorecardAbandonedFixtureEnterScoresBatAndBowlViewModel scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel = new ScorecardAbandonedFixtureEnterScoresBatAndBowlViewModel();

            // Form Collection

            string[] formCollectionFixtureId = formCollection.GetValues("ScorecardFixture.fixture_id");
            string[] formCollectionResultId = formCollection.GetValues("ScorecardHeaderDetail.result_id");
            string[] formCollectionScorecardEntered = formCollection.GetValues("ScorecardHeaderDetail.scorecard_entered");
            string[] formCollectionWonToss = formCollection.GetValues("ScorecardHeaderDetail.won_toss");
            string[] formCollectionBattedFirst = formCollection.GetValues("ScorecardHeaderDetail.batted_first");
            string[] formCollectionBatted = formCollection.GetValues("ScorecardHeaderDetail.batted");
            string[] formCollectionBowled = formCollection.GetValues("ScorecardHeaderDetail.bowled");

            // parse fixture

            int fixtureId = Int32.Parse(formCollectionFixtureId[0]);
            scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ScorecardFixture = fixtureRepository.GetFixture(fixtureId);

            // parse scorecard header

            scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ScorecardHeaderDetail = new ScorecardHeader()
            {
                fixture_id = fixtureId
            };

            if (formCollectionResultId[0] != "")
            {
                scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ScorecardHeaderDetail.result_id = Int32.Parse(formCollectionResultId[0]);
            }
            if (formCollectionScorecardEntered[0] != "")
            {
                scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ScorecardHeaderDetail.scorecard_entered = bool.Parse(formCollectionScorecardEntered[0]);
            }
            if (formCollectionWonToss[0] != "")
            {
                scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ScorecardHeaderDetail.won_toss = bool.Parse(formCollectionWonToss[0]);
            }
            if (formCollectionBattedFirst[0] != "")
            {
                scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ScorecardHeaderDetail.batted_first = bool.Parse(formCollectionBattedFirst[0]);
            }
            if (formCollectionBatted[0] != "")
            {
                scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ScorecardHeaderDetail.batted = bool.Parse(formCollectionBatted[0]);
            }
            if (formCollectionBowled[0] != "")
            {
                scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ScorecardHeaderDetail.bowled = bool.Parse(formCollectionBowled[0]);
            }

            // parse club players

            string[] playerFixtureId = formCollection.GetValues("item.fixture_id");
            string[] playerId = formCollection.GetValues("item.player_id");
            string[] batsmanNumber = formCollection.GetValues("item.batsman_number");
            string[] howOut = formCollection.GetValues("item.how_out");
            string[] fielderName = formCollection.GetValues("item.fielder_name");
            string[] bowlerName = formCollection.GetValues("item.bowler_name");
            string[] battingRuns = formCollection.GetValues("item.batting_runs");
            string[] bowlerNumber = formCollection.GetValues("item.bowler_number");
            string[] overs = formCollection.GetValues("item.overs");
            string[] maidens = formCollection.GetValues("item.maidens");
            string[] bowlingRuns = formCollection.GetValues("item.bowling_runs");
            string[] wickets = formCollection.GetValues("item.wickets");

            // club batsmen

            List<ClubFixturePlayer> scorecardClubPlayers = new List<ClubFixturePlayer>();

            for (int i = 0; i < scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ScorecardFixture.no_of_players; i++)
            {
                int currentFixture = Int32.Parse(playerFixtureId[i]);
                int currentPlayer = Int32.Parse(playerId[i]);

                ClubFixturePlayer clubBatsman = db.ClubFixturePlayers.Where(cfp => cfp.fixture_id == currentFixture && cfp.player_id == currentPlayer).Single();

                if (batsmanNumber[i] != "")
                {
                    clubBatsman.batsman_number = Int32.Parse(batsmanNumber[i]);
                }

                if (howOut[i] != "")
                {
                    clubBatsman.how_out = Int32.Parse(howOut[i]);
                }

                if (fielderName[i] != "")
                {
                    clubBatsman.fielder_name = fielderName[i];
                }

                if (bowlerName[i] != "")
                {
                    clubBatsman.bowler_name = bowlerName[i];
                }

                if (battingRuns[i] != "")
                {
                    clubBatsman.batting_runs = double.Parse(battingRuns[i]);
                }

                scorecardClubPlayers.Add(clubBatsman);
            }

            List<ClubFixturePlayer> sortedScorecardClubPlayers = scorecardClubPlayers.OrderBy(cfp => cfp.batsman_number).ToList();

            scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ClubBatsmen = sortedScorecardClubPlayers;

            // club bowlers

            List<ClubFixturePlayer> clubBowlers = new List<ClubFixturePlayer>();

            for (int j = scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ScorecardFixture.no_of_players; j < playerFixtureId.Length; j++)
            {
                int currentFixture = Int32.Parse(playerFixtureId[j]);
                int currentPlayer = Int32.Parse(playerId[j]);

                ClubFixturePlayer clubBowler = db.ClubFixturePlayers.Where(cfp => cfp.fixture_id == currentFixture && cfp.player_id == currentPlayer).Single();


                if (bowlerNumber[j - scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ScorecardFixture.no_of_players] != "")
                {
                    clubBowler.bowler_number = Int32.Parse(bowlerNumber[j - scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ScorecardFixture.no_of_players]);
                }

                if (overs[j - scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ScorecardFixture.no_of_players] != "")
                {
                    clubBowler.overs = double.Parse(overs[j - scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ScorecardFixture.no_of_players]);
                }

                if (maidens[j - scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ScorecardFixture.no_of_players] != "")
                {
                    clubBowler.maidens = double.Parse(maidens[j - scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ScorecardFixture.no_of_players]);
                }

                if (bowlingRuns[j - scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ScorecardFixture.no_of_players] != "")
                {
                    clubBowler.bowling_runs = double.Parse(bowlingRuns[j - scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ScorecardFixture.no_of_players]);
                }

                if (wickets[j - scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ScorecardFixture.no_of_players] != "")
                {
                    clubBowler.wickets = double.Parse(wickets[j - scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ScorecardFixture.no_of_players]);
                }

                clubBowlers.Add(clubBowler);
            }

            List<ClubFixturePlayer> sortedClubBowlers = clubBowlers.OrderBy(cfp => cfp.bowler_number).ToList();

            scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ClubBowlers = sortedClubBowlers;

            // club Other detail

            string[] formCollectionByes = formCollection.GetValues("ClubOtherDetail.byes");
            string[] formCollectionLegByes = formCollection.GetValues("ClubOtherDetail.leg_byes");
            string[] formCollectionWides = formCollection.GetValues("ClubOtherDetail.wides");
            string[] formCollectionNoBalls = formCollection.GetValues("ClubOtherDetail.no_balls");

            ClubOtherFixtureDetail clubOtherFixtureDetail = new ClubOtherFixtureDetail()
            {
                fixture_id = scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ScorecardFixture.fixture_id
            };

            if (formCollectionByes[0] != "")
            {
                clubOtherFixtureDetail.byes = double.Parse(formCollectionByes[0]);
            }

            if (formCollectionByes[0] == "")
            {
                clubOtherFixtureDetail.byes = 0;
            }

            if (formCollectionLegByes[0] != "")
            {
                clubOtherFixtureDetail.leg_byes = double.Parse(formCollectionLegByes[0]);
            }

            if (formCollectionLegByes[0] == "")
            {
                clubOtherFixtureDetail.leg_byes = 0;
            }

            if (formCollectionWides[0] != "")
            {
                clubOtherFixtureDetail.wides = double.Parse(formCollectionWides[0]);
            }

            if (formCollectionWides[0] == "")
            {
                clubOtherFixtureDetail.wides = 0;
            }

            if (formCollectionNoBalls[0] != "")
            {
                clubOtherFixtureDetail.no_balls = double.Parse(formCollectionNoBalls[0]);
            }

            if (formCollectionNoBalls[0] == "")
            {
                clubOtherFixtureDetail.no_balls = 0;
            }

            scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ClubOtherDetail = clubOtherFixtureDetail;

            // opponent players

            string[] opFixtureId = formCollection.GetValues("item.op_fixture_id");
            string[] opBatsmanNumber = formCollection.GetValues("item.op_batsman_number");
            string[] opBowlerNumber = formCollection.GetValues("item.op_bowler_number");
            string[] opPlayerName = formCollection.GetValues("item.op_player_name");
            string[] opOvers = formCollection.GetValues("item.op_overs");
            string[] opMaidens = formCollection.GetValues("item.op_maidens");
            string[] opBowlingRuns = formCollection.GetValues("item.op_bowling_runs");
            string[] opWickets = formCollection.GetValues("item.op_wickets");
            string[] opHowOut = formCollection.GetValues("item.op_how_out");
            string[] opFielderName = formCollection.GetValues("item.op_fielder_name");
            string[] opBowlerName = formCollection.GetValues("item.op_bowler_name");
            string[] opBattingRuns = formCollection.GetValues("item.op_batting_runs");

            // opponent bowlers

            List<OpponentFixturePlayer> opponentBowlers = new List<OpponentFixturePlayer>();

            for (int i = 0; i < opFixtureId.Length - scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ScorecardFixture.no_of_players; i++)
            {
                int batsmanId = Int32.Parse(opBatsmanNumber[i]);

                OpponentFixturePlayer opponentBowler = new OpponentFixturePlayer()
                {
                    op_fixture_id = scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ScorecardFixture.fixture_id,
                    op_batsman_number = batsmanId
                };

                if (opBowlerNumber[i] != "")
                {
                    opponentBowler.op_bowler_number = Int32.Parse(opBowlerNumber[i]);
                }

                if (opPlayerName[i] != "")
                {
                    opponentBowler.op_player_name = opPlayerName[i];
                }

                if (opOvers[i] != "")
                {
                    opponentBowler.op_overs = Int32.Parse(opOvers[i]);
                }

                if (opMaidens[i] != "")
                {
                    opponentBowler.op_maidens = Int32.Parse(opMaidens[i]);
                }

                if (opBowlingRuns[i] != "")
                {
                    opponentBowler.op_bowling_runs = Int32.Parse(opBowlingRuns[i]);
                }

                if (opWickets[i] != "")
                {
                    opponentBowler.op_wickets = Int32.Parse(opWickets[i]);
                }

                opponentBowlers.Add(opponentBowler);

            }

            List<OpponentFixturePlayer> sortedOpponentBowlers = opponentBowlers.OrderBy(ofp => ofp.op_bowler_number).ToList();

            scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.OpponentBowlers = sortedOpponentBowlers;

            // opponent Batsman

            List<OpponentFixturePlayer> scorecardOpponentPlayers = new List<OpponentFixturePlayer>();

            for (int i = opBowlerNumber.Length; i < opBatsmanNumber.Length; i++)
            {
                int currentFixture = Int32.Parse(opFixtureId[i]);
                int currentPlayerBatsmanNumber = Int32.Parse(opBatsmanNumber[i]);

                OpponentFixturePlayer opponentBatsman = new OpponentFixturePlayer
                {
                    op_fixture_id = currentFixture,
                    op_batsman_number = currentPlayerBatsmanNumber
                };

                if (opPlayerName[i] != "")
                {
                    opponentBatsman.op_player_name = opPlayerName[i];
                }

                //if (opHowOut[i - opBowlerNumber.Length] != "")
                //{
                //    opponentBatsman.op_how_out = Int32.Parse(opHowOut[i - opBowlerNumber.Length]);
                //}

                //if (opFielderName[i - opBowlerNumber.Length] != "")
                //{
                //    opponentBatsman.op_fielder_name = Int32.Parse(opFielderName[i - opBowlerNumber.Length]);
                //}

                //if (opBowlerName[i - opBowlerNumber.Length] != "")
                //{
                //    opponentBatsman.op_bowler_name = Int32.Parse(opBowlerName[i - opBowlerNumber.Length]);
                //}

                //if (opBattingRuns[i - opBowlerNumber.Length] != "")
                //{
                //    opponentBatsman.op_batting_runs = double.Parse(opBattingRuns[i - opBowlerNumber.Length]);
                //}

                scorecardOpponentPlayers.Add(opponentBatsman);
            }

            List<OpponentFixturePlayer> sortedScorecardOpponentPlayers = scorecardOpponentPlayers.OrderBy(ofp => ofp.op_batsman_number).ToList();

            scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.OpponentBatsmen = sortedScorecardOpponentPlayers;

            // club Other detail

            //string[] formCollectionOpByes = formCollection.GetValues("OpponentOtherDetail.byes");
            //string[] formCollectionOpLegByes = formCollection.GetValues("OpponentOtherDetail.leg_byes");
            //string[] formCollectionOpWides = formCollection.GetValues("OpponentOtherDetail.wides");
            //string[] formCollectionOpNoBalls = formCollection.GetValues("OpponentOtherDetail.no_balls");

            //OpponentOtherFixtureDetail opponentOtherFixtureDetail = new OpponentOtherFixtureDetail()
            //{
            //    fixture_id = scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ScorecardFixture.fixture_id
            //};

            //if (formCollectionOpByes[0] != "")
            //{
            //    opponentOtherFixtureDetail.byes = double.Parse(formCollectionOpByes[0]);
            //}

            //if (formCollectionOpByes[0] == "")
            //{
            //    opponentOtherFixtureDetail.byes = 0;
            //}

            //if (formCollectionOpLegByes[0] != "")
            //{
            //    opponentOtherFixtureDetail.leg_byes = double.Parse(formCollectionOpLegByes[0]);
            //}

            //if (formCollectionOpLegByes[0] == "")
            //{
            //    opponentOtherFixtureDetail.leg_byes = 0;
            //}

            //if (formCollectionOpWides[0] != "")
            //{
            //    opponentOtherFixtureDetail.wides = double.Parse(formCollectionOpWides[0]);
            //}

            //if (formCollectionOpWides[0] == "")
            //{
            //    opponentOtherFixtureDetail.wides = 0;
            //}

            //if (formCollectionOpNoBalls[0] != "")
            //{
            //    opponentOtherFixtureDetail.no_balls = double.Parse(formCollectionOpNoBalls[0]);
            //}

            //if (formCollectionOpNoBalls[0] == "")
            //{
            //    opponentOtherFixtureDetail.no_balls = 0;
            //}

            //scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.OpponentOtherDetail = opponentOtherFixtureDetail;

            return scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel;
        }


        public ScorecardAbandonedFixtureEnterScoresBatAndBowlViewModel ParseFormCollectionBowledOnly(FormCollection formCollection)
        {
            ScorecardAbandonedFixtureEnterScoresBatAndBowlViewModel scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel = new ScorecardAbandonedFixtureEnterScoresBatAndBowlViewModel();

            // Form Collection

            string[] formCollectionFixtureId = formCollection.GetValues("ScorecardFixture.fixture_id");
            string[] formCollectionResultId = formCollection.GetValues("ScorecardHeaderDetail.result_id");
            string[] formCollectionScorecardEntered = formCollection.GetValues("ScorecardHeaderDetail.scorecard_entered");
            string[] formCollectionWonToss = formCollection.GetValues("ScorecardHeaderDetail.won_toss");
            string[] formCollectionBattedFirst = formCollection.GetValues("ScorecardHeaderDetail.batted_first");
            string[] formCollectionBatted = formCollection.GetValues("ScorecardHeaderDetail.batted");
            string[] formCollectionBowled = formCollection.GetValues("ScorecardHeaderDetail.bowled");

            // parse fixture

            int fixtureId = Int32.Parse(formCollectionFixtureId[0]);
            scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ScorecardFixture = fixtureRepository.GetFixture(fixtureId);

            // parse scorecard header

            scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ScorecardHeaderDetail = new ScorecardHeader()
            {
                fixture_id = fixtureId
            };

            if (formCollectionResultId[0] != "")
            {
                scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ScorecardHeaderDetail.result_id = Int32.Parse(formCollectionResultId[0]);
            }
            if (formCollectionScorecardEntered[0] != "")
            {
                scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ScorecardHeaderDetail.scorecard_entered = bool.Parse(formCollectionScorecardEntered[0]);
            }
            if (formCollectionWonToss[0] != "")
            {
                scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ScorecardHeaderDetail.won_toss = bool.Parse(formCollectionWonToss[0]);
            }
            if (formCollectionBattedFirst[0] != "")
            {
                scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ScorecardHeaderDetail.batted_first = bool.Parse(formCollectionBattedFirst[0]);
            }
            if (formCollectionBatted[0] != "")
            {
                scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ScorecardHeaderDetail.batted = bool.Parse(formCollectionBatted[0]);
            }
            if (formCollectionBowled[0] != "")
            {
                scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ScorecardHeaderDetail.bowled = bool.Parse(formCollectionBowled[0]);
            }

            // parse club players

            string[] playerFixtureId = formCollection.GetValues("item.fixture_id");
            string[] playerId = formCollection.GetValues("item.player_id");
            string[] batsmanNumber = formCollection.GetValues("item.batsman_number");
            string[] bowlerNumber = formCollection.GetValues("item.bowler_number");
            string[] overs = formCollection.GetValues("item.overs");
            string[] maidens = formCollection.GetValues("item.maidens");
            string[] bowlingRuns = formCollection.GetValues("item.bowling_runs");
            string[] wickets = formCollection.GetValues("item.wickets");

            // club batsmen

            List<ClubFixturePlayer> scorecardClubPlayers = new List<ClubFixturePlayer>();

            for (int i = bowlerNumber.Length; i < playerId.Length; i++)
            {
                int currentFixture = Int32.Parse(playerFixtureId[i]);
                int currentPlayer = Int32.Parse(playerId[i]);

                ClubFixturePlayer clubBatsman = db.ClubFixturePlayers.Where(cfp => cfp.fixture_id == currentFixture && cfp.player_id == currentPlayer).Single();

                if (batsmanNumber[i - bowlerNumber.Length] != "")
                {
                    clubBatsman.batsman_number = Int32.Parse(batsmanNumber[i - bowlerNumber.Length]);
                }
                                
                scorecardClubPlayers.Add(clubBatsman);
            }

            List<ClubFixturePlayer> sortedScorecardClubPlayers = scorecardClubPlayers.OrderBy(cfp => cfp.batsman_number).ToList();

            scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ClubBatsmen = sortedScorecardClubPlayers;

            // club bowlers

            List<ClubFixturePlayer> clubBowlers = new List<ClubFixturePlayer>();

            for (int j = 0; j < bowlerNumber.Length; j++)
            {
                int currentFixture = Int32.Parse(playerFixtureId[j]);
                int currentPlayer = Int32.Parse(playerId[j]);

                ClubFixturePlayer clubBowler = db.ClubFixturePlayers.Where(cfp => cfp.fixture_id == currentFixture && cfp.player_id == currentPlayer).Single();


                if (bowlerNumber[j] != "")
                {
                    clubBowler.bowler_number = Int32.Parse(bowlerNumber[j]);
                }

                if (overs[j] != "")
                {
                    clubBowler.overs = double.Parse(overs[j]);
                }

                if (maidens[j] != "")
                {
                    clubBowler.maidens = double.Parse(maidens[j]);
                }

                if (bowlingRuns[j] != "")
                {
                    clubBowler.bowling_runs = double.Parse(bowlingRuns[j]);
                }

                if (wickets[j] != "")
                {
                    clubBowler.wickets = double.Parse(wickets[j]);
                }

                clubBowlers.Add(clubBowler);
            }

            List<ClubFixturePlayer> sortedClubBowlers = clubBowlers.OrderBy(cfp => cfp.bowler_number).ToList();

            scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ClubBowlers = sortedClubBowlers;
                        
            // opponent players

            string[] opFixtureId = formCollection.GetValues("item.op_fixture_id");
            string[] opBatsmanNumber = formCollection.GetValues("item.op_batsman_number");
            string[] opPlayerName = formCollection.GetValues("item.op_player_name");
            string[] opHowOut = formCollection.GetValues("item.op_how_out");
            string[] opFielderName = formCollection.GetValues("item.op_fielder_name");
            string[] opBowlerName = formCollection.GetValues("item.op_bowler_name");
            string[] opBattingRuns = formCollection.GetValues("item.op_batting_runs");

            // opponent Batsman

            List<OpponentFixturePlayer> scorecardOpponentPlayers = new List<OpponentFixturePlayer>();

            for (int i = 0; i < opBatsmanNumber.Length; i++)
            {
                int currentFixture = Int32.Parse(opFixtureId[i]);
                int currentPlayerBatsmanNumber = Int32.Parse(opBatsmanNumber[i]);

                OpponentFixturePlayer opponentBatsman = new OpponentFixturePlayer
                {
                    op_fixture_id = currentFixture,
                    op_batsman_number = currentPlayerBatsmanNumber
                };

                if (opPlayerName[i] != "")
                {
                    opponentBatsman.op_player_name = opPlayerName[i];
                }

                if (opHowOut[i] != "")
                {
                    opponentBatsman.op_how_out = Int32.Parse(opHowOut[i]);
                }

                if (opFielderName[i] != "")
                {
                    opponentBatsman.op_fielder_name = Int32.Parse(opFielderName[i]);
                }

                if (opBowlerName[i] != "")
                {
                    opponentBatsman.op_bowler_name = Int32.Parse(opBowlerName[i]);
                }

                if (opBattingRuns[i] != "")
                {
                    opponentBatsman.op_batting_runs = double.Parse(opBattingRuns[i]);
                }

                scorecardOpponentPlayers.Add(opponentBatsman);
            }

            List<OpponentFixturePlayer> sortedScorecardOpponentPlayers = scorecardOpponentPlayers.OrderBy(ofp => ofp.op_batsman_number).ToList();

            scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.OpponentBatsmen = sortedScorecardOpponentPlayers;

            // opponent other detail

            string[] formCollectionOpByes = formCollection.GetValues("OpponentOtherDetail.byes");
            string[] formCollectionOpLegByes = formCollection.GetValues("OpponentOtherDetail.leg_byes");
            string[] formCollectionOpWides = formCollection.GetValues("OpponentOtherDetail.wides");
            string[] formCollectionOpNoBalls = formCollection.GetValues("OpponentOtherDetail.no_balls");

            OpponentOtherFixtureDetail opponentOtherFixtureDetail = new OpponentOtherFixtureDetail()
            {
                fixture_id = scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ScorecardFixture.fixture_id
            };

            if (formCollectionOpByes[0] != "")
            {
                opponentOtherFixtureDetail.byes = double.Parse(formCollectionOpByes[0]);
            }

            if (formCollectionOpByes[0] == "")
            {
                opponentOtherFixtureDetail.byes = 0;
            }

            if (formCollectionOpLegByes[0] != "")
            {
                opponentOtherFixtureDetail.leg_byes = double.Parse(formCollectionOpLegByes[0]);
            }

            if (formCollectionOpLegByes[0] == "")
            {
                opponentOtherFixtureDetail.leg_byes = 0;
            }

            if (formCollectionOpWides[0] != "")
            {
                opponentOtherFixtureDetail.wides = double.Parse(formCollectionOpWides[0]);
            }

            if (formCollectionOpWides[0] == "")
            {
                opponentOtherFixtureDetail.wides = 0;
            }

            if (formCollectionOpNoBalls[0] != "")
            {
                opponentOtherFixtureDetail.no_balls = double.Parse(formCollectionOpNoBalls[0]);
            }

            if (formCollectionOpNoBalls[0] == "")
            {
                opponentOtherFixtureDetail.no_balls = 0;
            }

            scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.OpponentOtherDetail = opponentOtherFixtureDetail;

            return scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel;
        }
    }
}